module.exports = app => {
    const router = require('express').Router()

    const Category = require('../../models/Category')
    const Article = require('../../models/Article')
    const Hero = require('../../models/Hero')
    const Equip = require('../../models/Equip')
    const Advert = require('../../models/Advert')


    router.get('/news/init', async (req, res) => {
        const parent = await Category.findOne({
            name: '新闻公告'
        })
        const categories = await Category.find().where({ parent }).lean()

        const item_type = ["公告", "公告", "赛事", "视频", "视频", "赛事", "公告", "新闻", "公告", "赛事", "赛事", "新闻", "赛事", "赛事", "公告", "公告"]
        const item_title = ["10.9云顶之弈更新：新星系加入云顶", "10.9版本更新公告：FPX冠军皮肤来袭！", "LPL微信话题开启！赢千元Q币应援周边", "TES的水手与上流JDG之争第一赛区春冠到底花落谁家？", "2020未来战士皮肤预览：卢锡安剑姬潘森艾克开启峡谷时空之旅", "2020LPL春季赛决赛：此刻敬英雄", "FPX冠军荣耀宝箱活动即将上线", "虎牙直播《朋友请听题》FPX专场火热来袭", "百万云顶攻擂赛：挑战主播赢万元现金", "2020全球总决赛赛区名额分配，LPL与LEC增至4个", "2020季中冠军赛取消，全球总决赛仍在全力筹备中", "凤出东方 凰鸣四海！FPX冠军手办抢先看！", "LPL春季赛季后赛赛程正式公布", "2020LPL春季赛常规赛荣誉评定开启", "10.8云顶之弈更新：暗星法师泽拉斯降临", "10.8版本更新公告：死亡机制迎来重大调整"]
        const item_time = ["2020-06-17", "2020-06-16", "2020-06-16", "2020-06-16", "2020-06-16", "2020-06-16", "2020-06-16", "2020-06-15", "2020-06-15", "2020-06-15", "2020-06-15", "2020-06-15", "2020-06-15", "2020-06-15", "2020-06-15", "2020-06-14"]
        const item_time1 = item_time.map(i => i.substring(5, i.length))
        const newsList = item_title.map((i, index) => {
            return {
                category: categories[index % categories.length],
                type: item_type[index],
                title: i,
                time: item_time1[index]
            }
        })

        // 插入
        await Article.deleteMany({})
        await Article.insertMany(newsList)

        res.send(newsList)

    })


    // web端 newList 接口

    router.get('/news/list', async (req, res) => {
        const complex = await Article.find().limit(7)

        const parent = await Category.findOne({
            name: '新闻公告'
        }).populate({
            path: 'children',
            populate: {
                path: 'newsList',
                limit: 3
            }
        }).lean()
        res.send(parent)

        // 聚合查询
        // const categories = await Category.aggregate([
        //     // 聚合管道
        //     { $match: { parent: parent._id } }, // 过滤
        //     {
        //         $lookup: {  //关联
        //             from: 'articles',
        //             localField: 'categories',
        //             foreignField: '_id',
        //             as: 'newsList'
        //         }
        //     },
        //     {
        //         $addFields: {  // 修改
        //             newsList: { $slice: ['$newsList', 7] }
        //         }
        //     }
        // ])

    })

    // web端 heroesList 接口
    router.get('/heroes/list', async (req, res) => {
        const parent = await Category.findOne({
            name: '英雄类型'
        }).populate({
            path: 'children',
            populate: {
                path: 'heroesList',
                limit: 4
            }
        }).lean()
        res.send(parent)

        // 聚合查询
        // const categories = await Category.aggregate([
        //     // 聚合管道
        //     { $match: { parent: parent._id } }, // 过滤
        //     {
        //         $lookup: {  //关联
        //             from: 'heroes',
        //             localField: '_id',
        //             foreignField: 'position',
        //             as: 'heroList'
        //         }
        //     }
        // ])
    })



    // 获取所有英雄详情
    router.get('/hero/list', async (req, res) => {
        const detail = await Hero.find();
        res.send(detail)
    })

    // 获取所有装备详情
    router.get('/equip/list', async (req, res) => {
        const detail = await Equip.find()
        res.send(detail)
    })


    // 根据id获取文章详情
    router.get('/article/:id', async (req, res) => {
        const detail = await Article.findById(req.params.id)
        res.send(detail)
    })

    // 根据id获取装备详情
    router.get('/equip/:id', async (req, res) => {
        const detail = await Equip.findById(req.params.id)
        res.send(detail)
    })

    // 根据id获取英雄详情
    router.get('/hero/:id', async (req, res) => {
        const detail = await Hero.findById(req.params.id)
            .populate('position')
            .populate('advantages_equip')
            .populate('disadvantages_equip')
            .populate('partner')
            .populate('enemy')
        res.send(detail)
    })

    //获取轮播图列表
    router.get('/swiper', async (req, res) => {
        let items = await Advert.findOne({'name': '轮播图'})
        items = items.items.slice(0, 4)
        res.send(items)
    })


    app.use('/web/api', router)
}


